package com.qch.rabbitmq.demo;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageBuilder;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.core.MessagePropertiesBuilder;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @Author qch
 * @Date 2021/5/14
 */
public class ProducerApp {
    public static void main(String[] args) throws Exception {
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-rabbit.xml");
        RabbitTemplate template = applicationContext.getBean(RabbitTemplate.class);
        Message msg;
        MessagePropertiesBuilder messagePropertiesBuilder = MessagePropertiesBuilder.newInstance();
        messagePropertiesBuilder.setContentEncoding("gbk");
        messagePropertiesBuilder.setContentType(MessageProperties.CONTENT_TYPE_TEXT_PLAIN);
        msg = MessageBuilder.withBody("你好".getBytes("gbk"))
                .andProperties(messagePropertiesBuilder.build()).build();
        //在spring-rabbit.xml中配置路由键等
        template.send("ex.direct","routing.q1",msg);
        applicationContext.close();

    }
}
